﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace 学生成绩管理系统
{
    public partial class Frmstuxxcx : Form
    {
        public Frmstuxxcx()
        {
            InitializeComponent();
        }
        public Frmstuxxcx(string flag)
        {
            InitializeComponent();
            try
            {
                DataSet dsclass = StuentInfoOperation.getStuentInfo(new StuentInfoData());
                this.combClass.DataSource = dsclass.Tables[0];
                this.combClass.ValueMember = "所在班级";
                this.combClass.DisplayMember = "所在班级";
            }
            catch (Exception ex)
            {
                ex.ToString();
            }
            if (flag == "mod")
            {//修改按钮显示
                this.btnMod.Visible = true;
            }
            else if(flag=="del")
            {//删除按钮显示
                this.btnDel.Visible = true;
            }
           
        }
        public void bindDataGird()//数据绑定
        {
            string sno = this.TxtSno.Text.Trim();
            string name = this.txtName.Text.Trim();
            string sex = (string)this.combSex.SelectedItem;
            string classid = (string)this.combClass.SelectedValue;
            StuentInfoData data = new StuentInfoData();
            data.Sno = sno;
            data.Sname = name;
            data.Sex = sex;
            data.Classid = classid;
            try
            {
                DataSet ds = StuentInfoOperation.getStuentInfo(data);
                this.dataGrid1.DataSource = ds.Tables[0];
            }
            catch(Exception ex)
            {
                ex.ToString();
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {//学生信息查询
            bindDataGird();

        }
        private void btnMod_Click(object sender, EventArgs e)
        {
            string sno = this.TxtSno.Text.Trim();
            string name = this.txtName.Text.Trim();
            string sex = (string)this.combSex.SelectedItem;
            string classid = (string)this.combClass.SelectedValue;
            StuentInfoData data = new StuentInfoData();
            data.Sno = sno;
            data.Sname = name;
            data.Sex = sex;
            data.Classid = classid;
            DataSet ds = StuentInfoOperation.getStuentInfo(data);
            this.dataGrid1.DataSource = ds.Tables[0];
            if (this.dataGrid1.RowCount <= 0)
            {
                MessageBox.Show("请查询要修改的记录！", "提示");
                return;
            }
            int index = this.dataGrid1.CurrentRow.Index;
            if (index < 0)
            {//选择要修改的记录
                MessageBox.Show("请选择要修改的记录！", "提示");
                return;
            }
            else
            {
                Frmaddstudent modstu = new Frmaddstudent(ds.Tables[0].Rows[index]["学号"].ToString());
                modstu.ShowDialog(this);
            }
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            string sno = this.TxtSno.Text.Trim();
            string name = this.txtName.Text.Trim();
            string sex = (string)this.combSex.SelectedItem;
            string classid = (string)this.combClass.SelectedValue;
            StuentInfoData data = new StuentInfoData();
            data.Sno = sno;
            data.Sname = name;
            data.Sex = sex;
            data.Classid = classid;
            DataSet ds = StuentInfoOperation.getStuentInfo(data);
            this.dataGrid1.DataSource = ds.Tables[0];
            int index = this.dataGrid1.CurrentRow.Index;
            if (index < 0)
            {//必须选择要删除的记录
                MessageBox.Show("请选择要删除的记录！", "提示");
                return;
            }
            else
            {
                if(MessageBox.Show("确认要删除吗？","删除",MessageBoxButtons.YesNo)==DialogResult.Yes)
                {
                    string id = ds.Tables[0].Rows[index]["学号"].ToString();
                    try
                    {
                        if (StuentInfoOperation.deleteStuentInfo(id))
                        {
                            MessageBox.Show("删除成功！", "提示");
                            bindDataGird();
                        }
                        else
                        {
                            MessageBox.Show("删除失败！","错误");
                        }
                    }
                    catch(Exception ex)
                    {
                        ex.ToString();
                        MessageBox.Show("删除失败！","错误");
                    }
                }
            }
        }

        private void Frmstuxxcx_Load(object sender, EventArgs e)
        {
        }
    }
    //StuentInfoData
    public class StuentInfoData
    {
        private string sno = "";
        private string sname = "";
        private string sex = "";
        private string birthday = "";
        private string address = "";
        private string tel = "";
        private string classid = "";
        public string Sno//学号
        {
            get { return sno; }
            set { sno = value; }
        }
        public string Sname//学生姓名
        {
            get { return sname; }
            set { sname = value; }
        }
        public string Sex//学生性别
        {
            get { return sex; }
            set { sex = value; }
        }
        public string Birthday//出生日期
        {
            get { return birthday; }
            set { birthday = value; }
        }
        public string Address//地址
        {
            get { return address; }
            set { address = value; }
        }
        public string Tel//电话号码
        {
            get { return tel; }
            set { tel = value; }
        }
        public string Classid//班级名称
        {
            get { return classid; }
            set { classid = value; }
        }
    }
    //Stuent InfoOperation
    public class StuentInfoOperation
    {
        private static DataAccess dataAccess = new DataAccess();
        public static bool insertStuentInfo(StuentInfoData stuentInfoData)
        {//数据插入
            string sql = "insert into 学生信息表(学号,姓名,性别,出生日期,地址,电话,所在班级)values(" +
                "'" + stuentInfoData.Sno + "'," +
               "'" + stuentInfoData.Sname + "'," +
                "'" + stuentInfoData.Sex + "'," +
                "'" + stuentInfoData.Birthday + "'," +
               "'" + stuentInfoData.Address + "'," +
               "'" + stuentInfoData.Tel + "'," +
               "'" + stuentInfoData.Classid + "')";
            return dataAccess.ExecuteSQL(sql);//执行sql语句
        }
        public static bool updataStuentInfo(StuentInfoData stuentInfoData)
        {//修改数据
            string sql = "update 学生信息表 set " +
                "姓名='" + stuentInfoData.Sname + "'," +
                "性别='" + stuentInfoData.Sex + "'," +
                "出生日期='" + stuentInfoData.Birthday + "'," +
                "地址='" + stuentInfoData.Address + "'," +
                "电话='" + stuentInfoData.Tel + "'," +
                "所在班级='" + stuentInfoData.Classid + "' where 学号='" + stuentInfoData.Sno + "'";
            return dataAccess.ExecuteSQL(sql);//执行sql语句

        }
        public static bool deleteStuentInfo(string sno)
        {//删除数据
            string sql = String.Format("delete 学生信息表 where 学号='{0}'",sno);
            return dataAccess.ExecuteSQL(sql);//执行sql语句
        }
        public static DataSet getStuentInfo(StuentInfoData stuentInfoData)
        {//数据查询
            string condition = "";//查询的条件
            if(stuentInfoData.Sno!=null&&""!=stuentInfoData.Sno)
            {
                condition += " and 学号='" + stuentInfoData.Sno + "'";
            }
            if(stuentInfoData.Sname!=null&&""!=stuentInfoData.Sname)
            {
                condition += " and 姓名 like'%" + stuentInfoData.Sname + "%'";
            }
            if (stuentInfoData.Sex != null && "" != stuentInfoData.Sex)
            {
                condition += " and 性别='" + stuentInfoData.Sex + "'";
            }
            if(stuentInfoData.Classid!=null&&""!=stuentInfoData.Classid)
            {
                condition += " and 所在班级='" + stuentInfoData.Classid + "'";
               
            }
            string sql = @"select [学号],[姓名],[性别],[出生日期],[所在班级],[电话],[地址] from 学生信息表 where 1=1" + condition;
            return dataAccess.GetDataSet(sql, "select * from 学生信息表");//执行sql语句
        }
    }
}